package com.blue.common.filter;

import com.blue.common.constant.PathConstant;
import com.blue.common.interceptor.RequestWrapper;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpMethod;

import java.io.IOException;

/**
 * 解决Post请求体只能获取一次问题过滤器
 *
 * @author nanjie.chen
 * Created on 2023/10/4
 */
public class RequestFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        if (HttpMethod.POST.name().equals(request.getMethod()) && !PathConstant.ignorePathList.contains(request.getRequestURI())) {
            //如果是POST并且非文件上传接口就走自己的继承的HttpServletRequestWrapper类请求，否则走正常的请求
            filterChain.doFilter(new RequestWrapper(request), servletResponse);
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }
}